import { findIndex, uniqueId, cloneDeep } from 'lodash'
import { getCurrentInstance } from 'vue'
import { Messages, confirm } from '@Config/config'
import { message } from 'ant-design-vue'
import { favoriteAdd, favoriteDel } from '@Config/server/api'
import { useRoute } from 'vue-router'
import { useWin } from '@Hooks/win'
import CreateFavoriteFolder from '@Page/favorites/components/createFavoriteFolder'

export const instanceFn = () => {
  let { proxy } = getCurrentInstance()
  let route = useRoute()

  /**
   * 创建文件夹
   */
  let createFolder = async (params) => {
    let lid = uniqueId('local_')
    proxy.tableData.data.source.push({
      fileType: 0,
      name: '等待创建',
      local_loading: true,
      id: lid,
    })
    let _r = await favoriteAdd({
      ...params,
    })
    let _index = findIndex(proxy.tableData.data.source, { id: lid })

    if (_r.code == 0) {
      proxy.tableData.data.source.splice(_index, 1, { ..._r.data })

      Messages.add(message.success('创建文件夹成功'))
    } else {
      proxy.tableData.data.source.splice(_index, 1)
      Messages.add(message.error(`创建文件夹失败[${_r.msg}]`))
    }

    return _r
  }

  let editFolder = async ({ title, description, id }) => {
    let _index = findIndex(proxy.tableData.data.source, { id: id })
    if (_index >= 0) {
      proxy.tableData.data.source[_index].local_loading = true
    }
    let _r = await favoriteAdd({
      title,
      description,
      id,
    })
    proxy.tableData.data.source[_index].local_loading = false

    if (_r.code == 0) {
      let newItem = {
        ...cloneDeep(proxy.tableData.data.source[_index]),
        title,
        description,
        id,
        name: title,
      }
      proxy.tableData.data.source.splice(_index, 1, newItem)
      Messages.add(message.success(`编辑成功`))
    } else {
      Messages.add(message.error(`编辑失败[${_r.msg}]`))
    }
  }

  /**
   *
   * @param {*} targets[]:需要删除备份文件夹的对象
   */
  let deleteFavoriteFolder = async (targets) => {
    await confirm({
      title: <span class="red">警告</span>,
      content: <span>确定要删除备份文件夹吗？</span>,
    })
    let ids = targets.map((x) => x.id)

    targets.map((x) => {
      let _index = findIndex(proxy.tableData.data.source, { id: x.id })
      if (_index >= 0) {
        proxy.tableData.data.source[_index].local_loading = true
      }
    })
    let _r = await favoriteDel({ ids })

    targets.map((x) => {
      let _index = findIndex(proxy.tableData.data.source, { id: x.id })
      if (_index >= 0) {
        proxy.tableData.data.source[_index].local_loading = false
      }
    })

    if (_r.code == 0) {
      targets.map((x) => {
        let _index = findIndex(proxy.tableData.data.source, { id: x.id })
        proxy.tableData.data.source.splice(_index, 1)
      })
      Messages.add(message.success(`删除成功`))
    } else {
      Messages.add(message.error(`删除失败[${_r.msg}]`))
    }
  }

  let [favoriteWin] = useWin({
    component: (
      <CreateFavoriteFolder
        onResult={async ({ title, description, record }) => {
          if (record.id != undefined) {
            //编辑
            await editFolder({ title, description, id: record.id })
          } else {
            //新建
            let _r = await createFolder({ id: 0, title, description })
          }
        }}
      />
    ),
  })

  //添加备份文件夹
  let createFavoriteFolder = async () => {
    favoriteWin({
      attr2: {
        title: '新建备份文件夹',
      },
    })
  }

  //编辑备份文件夹基本信息

  let editFavoriteFolder = async (e) => {
    favoriteWin({
      params: {
        record: e,
      },
      attr2: {
        title: '编辑备份文件夹',
      },
    })
  }

  return {
    createFolder,
    editFolder,
    deleteFavoriteFolder,
    createFavoriteFolder,
    editFavoriteFolder,
  }
}
